﻿using System;
using System.Web;
using System.Web.Security;
using INHollandair.Core.Models;
using INHollandair.Core.Services;

namespace INHollandair.Account
{
    public partial class ConfirmRegistration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pnl_RegistrationSucces.Visible = false;

            UserService svc = new UserService();
            string registerUrl = (Request.QueryString["RegisterUrl"] != null) ? Server.HtmlEncode(Request.QueryString["RegisterUrl"]) : String.Empty;
            
            if (Session["RegistrationSuccesful"] != null)
            {
                pnl_RegistrationSucces.Visible = true;
                lbl_User.Text = HttpContext.Current.User.Identity.Name;
            }
            else if(!registerUrl.Equals(string.Empty))
            {
                RegisterURL url = svc.GetRegisterUrlByUrl(registerUrl);

                if (url != null)
                {
                    User newUser = svc.GetUserById(url.UserId);
                    newUser.IsActivated = true;
                    svc.UpdateUser(newUser);
                    svc.RemoveRegisterUrl(url);

                    FormsAuthentication.SetAuthCookie(newUser.Name, false);
                    Session["RegistrationSuccesful"] = true;
                    Response.Redirect("~/Account/ConfirmRegistration.aspx");
                }
                else
                {
                    Response.Redirect("~/Default.aspx"); //gebruiker zit te prutsen met strings
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx"); //gebruiker heeft hier niks te zoeken
            }
        }
    }
}